/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 * MIFSS  - content storage system
 * 
 *
 * @uthors: uros.kristan@gmail.com (Uroš Kristan ) Urosk.NET
 *         jernej.svigelj@gmail.com (Jernej Švigelj) 
 */
package net.monare.mifss.configurations.pojo;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

import java.util.*;

@XStreamAlias("storageDef")
public class StorageDef {

    @XStreamAsAttribute
    private String chainedToStorageName;

    private List<ContentPoolDef> contentPoolDefs = new ArrayList<ContentPoolDef>();

    @XStreamAsAttribute
    private String dataSourceName;

    private String description;

    @XStreamAsAttribute
    private String name;

    @XStreamAsAttribute
    private StorageMode mode;

    private transient StorageSetDef storageSetDef;

    @XStreamAsAttribute
    private String title;

    private String transformationHandlerName;

    private Set<Property> properties = new HashSet<>();

    private Set<ApiKey> apiKeys = new HashSet<>();

    public StorageSetDef getStorageSetDef() {
        return storageSetDef;
    }

    public void setStorageSetDef(StorageSetDef storageSetDef) {
        this.storageSetDef = storageSetDef;
    }

    public String getChainedToStorageName() {
        return chainedToStorageName;
    }

    public void setChainedToStorageName(String chainedToStorageName) {
        this.chainedToStorageName = chainedToStorageName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Property> getProperties() {

        return properties;
    }

    public void removeProperty(String propertyName){

        Iterator propertiesIterator = properties.iterator();

        while (propertiesIterator.hasNext()) {

            Property property = (Property)propertiesIterator.next();

            if(property.getName().equals(propertyName)){
                propertiesIterator.remove();
            }

        }

    }

    public void setProperties(Set<Property> properties) {
        this.properties = properties;
    }

    public Set<ApiKey> getApiKeys() {
        return apiKeys;
    }

    public void setApiKeys(Set<ApiKey> apiKeys) {
        this.apiKeys = apiKeys;
    }

    public void addContentPoolDef(ContentPoolDef contentPoolDef) {
        getContentPoolDefs().add(contentPoolDef);

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        StorageDef that = (StorageDef) o;

        if (!name.equals(that.name)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    public ContentPoolDef getActivePoolDef() {

        for (ContentPoolDef pool : contentPoolDefs)
            if (pool.isWriteActive())
                return pool;

        return null;
    }

    public ContentPoolDef getContentPoolDefFromId(int id) {

        for (ContentPoolDef pool : contentPoolDefs)
            if (pool.getId() == id)
                return pool;

        return null;
    }

    // TODO refactor getContentPoolDefs method to be consistent with method name
    // getPoolDefinitionFromId
    public List<ContentPoolDef> getContentPoolDefs() {
        return contentPoolDefs;
    }

    public String getDataSourceName() {
        return dataSourceName;
    }

    public String getDescription() {
        return description;
    }

    public StorageMode getMode() {
        return mode;
    }

    public String getTitle() {
        return title;
    }

    public String getTransformationHandlerName() {
        return transformationHandlerName;
    }

    public boolean isChained() {
        return (chainedToStorageName != null && !"".equals(chainedToStorageName));
    }

    public boolean isTransformable() {
        return transformationHandlerName != null;
    }

    public void setContentPoolDefs(List<ContentPoolDef> contentPoolDefs) {
        this.contentPoolDefs = contentPoolDefs;
    }

    public void setDataSourceName(String dataSourceName) {
        this.dataSourceName = dataSourceName;
    }


    public void setDescription(String description) {
        this.description = description;
    }

    public void setMode(StorageMode mode) {
        this.mode = mode;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setTransformationHandlerName(String transformationHandlerName) {
        this.transformationHandlerName = transformationHandlerName;
    }

    public void addApiKey(ApiKey apiKey) {
        if (apiKeys == null)
            apiKeys = new HashSet<ApiKey>();

        apiKeys.add(apiKey);
    }

}
